#!/bin/sh

DOMAIN="laikas"

find ./ -iname "*.py" \
        -not -wholename "./locale/*" \
        > .localefiles

for locale in ./locale/* ; do
    echo "Generating $locale ..."
    if [ -f $locale/LC_MESSAGES/$DOMAIN.po ]; then
        mv $locale/LC_MESSAGES/$DOMAIN.po $locale/LC_MESSAGES/$DOMAIN.po.old
        xgettext -f .localefiles --from-code=UTF-8  --keyword="_" --sort-output \
            -o $locale/LC_MESSAGES/$DOMAIN.po.new --force-po
        sed -i 's/CHARSET/UTF-8/g' $locale/LC_MESSAGES/$DOMAIN.po.new
        msgmerge $locale/LC_MESSAGES/$DOMAIN.po.old $locale/LC_MESSAGES/$DOMAIN.po.new \
            -o $locale/LC_MESSAGES/$DOMAIN.po

        if [ -f $locale/LC_MESSAGES/$DOMAIN.po ]; then
            rm $locale/LC_MESSAGES/$DOMAIN.po.old $locale/LC_MESSAGES/$DOMAIN.po.new
        else
            # If .po file was not merged, then leave old one.
            echo "Error: Can not merge .po files."
            mv $locale/LC_MESSAGES/$DOMAIN.po.old $locale/LC_MESSAGES/$DOMAIN.po
            rm $locale/LC_MESSAGES/$DOMAIN.po.new
        fi
    else
        xgettext -f .localefiles --from-code=UTF-8  --keyword="_" --sort-output \
            -o $locale/LC_MESSAGES/$DOMAIN.po --force-po
    fi
    msgfmt $locale/LC_MESSAGES/$DOMAIN.po -o $locale/LC_MESSAGES/$DOMAIN.mo
done

rm .localefiles
echo "Done."


